@equinor/fusion-framework-module-event
This package is meant for dispatching events between modules (siblings) and cross instances (parent|adjunct)
Base on the native node/web js event system, but the dispatcher is async
for easier handling of cancelable
events.
NOTE that creating a cancelable
event without awaiting resolution, will not respect the preventDefault
behavior!
Configuration
const configurator = (config) => {
delete config.event.onBubble
config.event.onDispatch = (e: FrameworkEvent) => {
if(!allow_event(e)){
e.preventDefault();
}
}
}
Declaring events
import { ModuleEvent } from '@equinor/fusion-framework-module-event';
declare module '@equinor/fusion-framework-module-event' {
interface ModuleEventMap {
'someEvent': FrameworkEvent<FrameworkEventInit<MyDataObject, MySource>>;
}
}
Custom events
import { ModuleEvent } from '@equinor/fusion-framework-module-event';
type CustomFrameworkEventInit = FrameworkEventInit<MyDataObject, MySource>;
class MyCustomEvent extends FrameworkEvent<CustomFrameworkEventInit, 'myCustomEvent'> {
constructor(init: CustomFrameworkEventInit) { }
}
declare module '@equinor/fusion-framework-module-event' {
interface ModuleEventMap {
'myCustomEvent': MyCustomEvent;
}
}
Usage
Handle a single event type
const teardown = modules.event.addEventListener('someEvent', (event) => console.log(event));
teardown();
Dispatch event
const event = await modules.event.dispatchEvent(
'myEvent',
{
detail: 'some detail',
canBubble: false,
cancelable: true
}
);
const event = new MyCustomFrameworkEvent(
'myCustomEvent',
{
detail: 'some detail',
canBubble: false,
cancelable: true
}
);
await modules.event.dispatchEvent(myEvent);
if(!event.defaultPrevent){
doSomeAction();
}
Subscribe to all events
note that when subscribing to events, it does not allow side-effects, like preventDefault
and stopPropagation
const subscription = modules.event.subscribe(console.log);
subscription.add(
modules.event.subscribe({
next: (event) => console.log(event),
error: (err) => console.error(err),
complete: () => 'event provider disposed'
})
);
subscription.unsubscribe();